package com.tnr.scgcxx.controller.safty;

import com.tnr.scgcxx.base.Constants;
import com.tnr.scgcxx.base.CurrUser;
import com.tnr.scgcxx.base.Result;
import com.tnr.scgcxx.dto.UserDto;
import com.tnr.scgcxx.exception.BusinessException;
import com.tnr.scgcxx.exception.SysException;
import com.tnr.scgcxx.service.safty.LoginServce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/safty/login")
@SessionAttributes(Constants.SESSION_ATTR_CURR_USER)//说明什么属性存在session中
public class LoginController {
    @Autowired
    private LoginServce loginServce;

     @PostMapping("/user")
     public Result login(@RequestBody  UserDto userDto, HttpSession session){
         try {
             CurrUser currser= loginServce.checklogin(userDto);//返回当前用户
             //model默认存在request中存放信息
             session.setAttribute(Constants.SESSION_ATTR_CURR_USER,currser);

             return Result.success("登录成功");
         }
         catch (BusinessException e) {

             return Result.fail(e.getMessage());
         }
         catch (SysException e){

             return Result.fail("系统繁忙");

         }

     }
}
